<template>
  <div class='container'>
    <!-- 表单 -->
    <el-row type="flex" class="row-bg" justify="end">
      <el-col :span="22"><div class="grid-content bg-purple">
          <el-form :inline="true" :model="requestParameter"  class="demo-form-inline" ref="form">
          <el-form-item label="标签名称" prop="tagName">
            <el-input v-model="requestParameter.tagName"></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="state" style="padding-left:50px">
            <el-select v-model="requestParameter.state" placeholder="请选择">
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="purge">清除</el-button>
            <el-button type="primary" @click="doSearch">搜索</el-button>
          </el-form-item>
        </el-form>
        </div></el-col>
      <el-col :span="2"><div class="grid-content bg-purple">
        <el-button type="success" icon="el-icon-edit" @click="showDialog=true">新增标签</el-button>
        </div></el-col>
    </el-row>

<!-- 消息提示 -->
   <el-alert
   :title="`数据一共 ${total} 条`"
    type="info"
    show-icon
    :closable="false"
    >
  </el-alert>

  <!-- table表格 -->
     <el-table
      :data="tableData"
      style="width: 100%">
         <el-table-column
          prop="listId"
        label="序号"
        >
      </el-table-column>
      <el-table-column
        prop="subjectName"
        label="所属学科"
        width="243px">
      </el-table-column>
      <el-table-column
        prop="tagName"
        label="标签名称"
        width="243px">
      </el-table-column>
      <el-table-column
        prop="username"
        label="创建者"
        width="243px">
      </el-table-column>
       <el-table-column
        prop="addDate"
        label="创建日期"
        width="243px">
          <template v-slot={row}>
            {{dayjs(row.addDate).format('YYYY-MM-DD HH:mm:ss')}}
          </template>
      </el-table-column>
       <el-table-column
        prop="state"
        label="状态"
        width="243px">
          <template v-slot={row}>
            {{ row.state ? "已启用" :  "已禁用"  }}
          </template>
      </el-table-column>
       <el-table-column
        label="操作"
        width="150px">
        <template v-slot={row}>
        <div v-if="row.state">
          <el-button type="text" @click="buttonDisable(row)">禁用</el-button>
          <el-button type="text" @click="revise(row.id)">修改</el-button>
          <el-button type="text" @click="deleteList(row.id)">删除</el-button>
        </div>
         <div v-else>
          <el-button type="text" @click="buttonDisable(row)">启用</el-button>
          <el-button type="text" :disabled="true">修改</el-button>
          <el-button type="text" :disabled="true">删除</el-button>
         </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
     <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-size="requestParameter.pagesize"
      :page-sizes="[5, 10, 20,50]"
      :current-page="requestParameter.page"
      layout=" prev, pager, next, sizes,jumper"
      background
      :total="total">
    </el-pagination>

    <!-- 对话框 -->
    <tagsAdd
     :showDialog.sync="showDialog"
     @addTableList="addTableList"
     :isEdit.sync="isEdit"
     :id="id"
   ></tagsAdd>
  </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/tags.js'
import tagsAdd from '@/module-hmmm/components/tags-add.vue'
export default {
  name: 'tags',
  data () {
    return {
      tableData: [],
      requestParameter: {
        page: 1,
        pagesize: 10,
        tagName: null,
        state: null,
        subjectID: null
      },
      total: null,
      showDialog: false,
      id: null,
      editList: {},
      isEdit: ''
    }
  },
  components: {
    tagsAdd
  },
  created () {
    if (this.$route.query.id || this.$route.query.name) {
      this.requestParameter.subjectID = this.$route.query.id
      // this.requestParameter.tagName = this.$route.query.name
    }
    console.log(this.requestParameter)
    this.getTableList()
  },
  methods: {
    // 表格数据请求
    async getTableList () {
      const { data } = await list(this.requestParameter)
      data.items.forEach((item, index) => {
        item.listId = index + 1
      })
      this.tableData = data.items
      this.total = data.counts
    },
    // 当前页数据变化
    handleSizeChange (size) {
      this.requestParameter.pagesize = size
      this.getTableList()
    },
    // 页码变化
    handleCurrentChange (current) {
      this.requestParameter.page = current
      this.getTableList()
    },
    // 清除按钮,清空表单数据
    purge () {
      this.$refs.form.resetFields()
    },
    // 搜索按钮
    doSearch () {
      this.getTableList()
    },
    // 添加
    addTableList () {
      this.getTableList()
      this.requestParameter.page = 1
    },
    // 修改
    async revise (id) {
      this.showDialog = true
      this.isEdit = true
      this.id = id
    },
    // 删除
    deleteList (id) {
      this.$confirm('此操作将永久删除该标签, 是否继续', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id })
        if (this.tableData.length === 1) {
          this.requestParameter.page = 1
        }
        this.getTableList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      })
    },
    // 按钮禁用
    async buttonDisable (row) {
      if (row.state === 1) {
        row.state = 0
      } else {
        row.state = 1
      }
      await changeState({ id: row.id, state: row.state })
      this.$message({
        message: '操作成功',
        type: 'success'
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.container{
  margin: 10px;
  padding: 20px;
  background-color: #ffffff;
  .el-table{
    margin-top: 20px;
    margin-bottom: 20px;
  }
  .el-pagination{
    display: flex;
    justify-content: flex-end;
  }
}
</style>
